package servlet;

import daoImpl.FloorDaoImpl;
import daoImpl.UserDaoImpl;
import entity.Floor;
import entity.User;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by miaoyu on 2017/7/15.
 */
public class PublishFloorServlet extends HttpServlet {
    private static final String UPLOAD_DIRECTORY = "floor";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("userID",1);
        req.setAttribute("postID",1);
        int postID = Integer.valueOf(req.getAttribute("postID").toString());
        int userID = Integer.valueOf(req.getSession().getAttribute("userID").toString());

        //获取页面中编辑部分的内容
        String content = req.getParameter("content");
        if(content==null){
            content="";
        }


        //获取当前用户
        UserDaoImpl userDao = new UserDaoImpl();
        User user = new User();
        user =userDao.queryUserByID(userID);

        //获取当前时间戳
        Date now = Calendar.getInstance().getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timestamp = sdf.format(now);
        timestamp = timestamp.replace(':','-');

        // 配置上传参数
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);


        //构建目录
        String storagePart = UPLOAD_DIRECTORY + File.separator + user.getUsername() + File.separator + timestamp;
        String uploadPath = req.getSession().getServletContext().getRealPath("/") + storagePart;
        // 如果目录不存在则创建
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        //将帖子内容写入文件
        String fileName = "content.txt";
        String filePath = uploadPath + File.separator +  fileName;
        System.out.println(filePath);
        try{
            File file = new File(filePath);
            if(!file.exists()){
                file.createNewFile();
            }

            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            bw.write(content);
            bw.flush();
            bw.close();

            //存入数据库
            Floor floor = new Floor();
            floor.setUserID(userID);
            floor.setTime(timestamp);
            storagePart= storagePart + File.separator + fileName;
            storagePart = storagePart.replace("\\","\\\\");
            floor.setContent(storagePart);
            floor.setPostID(postID);
            //获取楼层数
            FloorDaoImpl floorDao = new FloorDaoImpl();
            floor.setFloorNum(floorDao.getNewFloorNum(postID));
            floorDao.addFloor(floor);
        }catch (Exception e){
            e.printStackTrace();
        }


        resp.sendRedirect("Floors.jsp");
        System.out.println(req.getRequestURL());
        //resp.sendRedirect(req.getRequestURL().toString());
    }
}
